package com.pikaqiu.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pikaqiu.warehouse.domain.common.ReqCommonParams;
import com.pikaqiu.warehouse.domain.dto.SpecialUserDto;
import com.pikaqiu.warehouse.domain.entity.Employee;
import com.pikaqiu.warehouse.domain.req.SpecialUserForm;
import com.pikaqiu.warehouse.mapper.EmployeeMapper;
import com.pikaqiu.warehouse.service.EmployeeService;
import com.pikaqiu.warehouse.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【employee(供应商表)】的数据库操作Service实现
* @createDate 2024-08-18 13:31:18
*/
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
    implements EmployeeService{

    @Resource
    private UserService userService;

    @Override
    public List<SpecialUserDto> listCompanyEmployee() {
        ReqCommonParams reqCommonParams = userService.checkUser();
        Integer companyId = reqCommonParams.getCompanyId();
        List<Employee> employeeList = list(new QueryWrapper<Employee>().eq("company_id", companyId));
        return employeeList.stream().map(SpecialUserDto::new).collect(Collectors.toList());
    }

    @Override
    public void saveEmployee(SpecialUserForm specialUserForm) {
        ReqCommonParams reqCommonParams = userService.checkUser();
        Employee employee = specialUserForm.toEmployee();
        employee.setCompanyId(reqCommonParams.getCompanyId());
        if (specialUserForm.getId() != null) {
            employee.setUpdateBy(reqCommonParams.getUserId().toString());
        } else {
            employee.setCreateBy(reqCommonParams.getUserId().toString());
        }
        save(employee);
    }
}




